<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/3/3
 * Time: 下午6:14
 */

namespace App\HttpController;


use App\Model\Protobuf\Server;
use AutoMsg\ConnectingResult;
use EasySwoole\Core\Http\AbstractInterface\Controller;
use EasySwoole\Core\Http\Message\Status;
use EasySwoole\Core\Swoole\ServerManager;
use think\Db;
use EasySwoole\Core\Component\Di;
use GPBMetadata\Protomsg;
use EasySwoole\Config;
class ReadProto extends Controller
{

    //测试路径 /index.html
    function index()
    {
        $res = opendir('./data/client');
        while ($row = readdir($res)) {
           // var_dump($row);
            if($row == '.' || $row == '..'){
                continue;
            }
            // $str = file_get_contents('./data/server/' .$row);
            $str = file_get_contents('./data/client/' .$row);

            //获取文件名字
            $MsgId = basename($row,'.txt');
            $arr['MsgId'] = $MsgId;
//            $arr['str'] = $str;
            $arr['type'] = 'server';
            // var_dump($arr);
            // 插入mongo数据库
            $rs = Db::table('protobuf')->insert($arr);
            var_dump($rs);
        }
        // $file = scandir('./data/server');
        // var_dump($file);
        $this->response()->write($arr);

    }
    function jiexi(){
        $server = new Server();
        $data = file_get_contents('./data/client/1066.txt');

        $MsgId = 1066;
        $protoConf = Config::getInstance()->getConf('proto');
        $serverConf = $protoConf['server'];
        $method = $serverConf[$MsgId]['method'];
        $server->$method($data);

        $this->response()->write('11');
    }
    //测试路径 /test/index.html
    function test()
    {
        $ip = ServerManager::getInstance()->getServer()->connection_info($this->request()->getSwooleRequest()->fd);
        var_dump($ip);
        $ip2 = $this->request()->getHeaders();
        var_dump($ip2);
        $this->response()->write('index controller test');
    }

    /*
     * protected 方法对外不可见
     *  测试路径 /hide/index.html
     */
    protected function hide()
    {
        var_dump('this is hide method');
    }

    protected function actionNotFound($action): void
    {
        $this->response()->withStatus(Status::CODE_NOT_FOUND);
        $this->response()->write("{$action} is not exist");
    }

    function a()
    {
        $this->response()->write('index controller router');
    }

    function a2()
    {
        $this->response()->write('index controller router2');
    }

    function test2(){
        $this->response()->write('this is controller test2 and your id is '.$this->request()->getRequestParam('id'));
    }
}